package fina2.dcs.fis;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import fina2.dcs.context.ContextHelper;
import fina2.dcs.i18n.LanguageSession;
import fina2.dcs.system.SysString;
import fina2.dcs.system.SysStringLocal;

@Stateful
@Name("bankGroupSession")
@LocalBinding(jndiBinding = "BankGroupSession/local")
@SuppressWarnings("unchecked")
@Scope(ScopeType.SESSION)
@AutoCreate
public class BankGroupSession implements BankGroupLocal,Serializable {

	@PersistenceContext
	private EntityManager em;
	
	@Logger
	private Log log;
	
	private InitialContext ic = null;

	private SysStringLocal sysStringLocal = null;
	private CriterionLocal critLocal = null;

	public void init() {

	}

	@Remove
	@Destroy
	public void remove() {

	}

	public List<BankGroup> getGroupsByCriterionId(BigDecimal id) {
		List<BankGroup> bg = null;
		try {
			ic = ContextHelper.getInitialContext();
			sysStringLocal = ContextHelper.injectSysStringLocal(ic);
			bg = em.createQuery("FROM " + BankGroup.class.getName() + " b WHERE RTRIM(b.criterionId)=? ORDER BY b.code ASC").setParameter(1, id).getResultList();
			for (int i = 0; i < bg.size(); i++) {
				List<SysString> descs = sysStringLocal.getAllValues(bg.get(i).getNameStrId().toString(), LanguageSession.langId);
				for (int j = 0; j < descs.size(); j++) {
					if (bg.get(i).getNameStrId().toString().trim().equals(descs.get(j).getPk().getId().toString().trim())) {
						bg.get(i).setDescription(descs.get(j).getValue());
						break;
					}

				}
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return bg;
	}

	public BankGroup getBankGroupById(BigDecimal id) {
		BankGroup bg = null;
		try {
			ic = ContextHelper.getInitialContext();
			sysStringLocal = ContextHelper.injectSysStringLocal(ic);
			bg = em.find(BankGroup.class, id);
			List<SysString> descs = sysStringLocal.getAllValues(bg.getNameStrId().toString(), LanguageSession.langId);
			for (int j = 0; j < descs.size(); j++) {
				if (bg.getNameStrId().toString().trim().equals(descs.get(j).getPk().getId().toString().trim())) {
					bg.setDescription(descs.get(j).getValue());
					break;
				}
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return bg;
	}

	public BankGroup findByCode(String code) {
		BankGroup bg = null;
		try {
			bg = (BankGroup) em.createQuery("FROM " + BankGroup.class.getName() + " bg WHERE RTRIM(bg.code)=:code").setParameter("code", code.trim()).getSingleResult();
		} catch (NoResultException ex) {
			log.info("Could not find Bank Group " + code);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return bg;
	}

	public List<BankGroup> getDefaultBankGroups() {
		List<BankGroup> defBgList = new ArrayList<BankGroup>();
		try {
			ic = ContextHelper.getInitialContext();
			sysStringLocal = ContextHelper.injectSysStringLocal(ic);
			critLocal = ContextHelper.injectCriterionLocal(ic);
			Criterion defaultCrit = critLocal.getDefaultCriterion();
			defBgList = getGroupsByCriterionId(defaultCrit.getId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return defBgList;
	}

}
